﻿using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using UltimateNavigationFramework.Loaders;
using UltimateNavigationFramework.UriNavigation;

namespace UltimateNavigationFramework
{
    #region Bootstrapper
    public abstract class UltimateUnityBootstrapper : UnityBootstrapper
    {
        #region Create view catalog
        /// <summary>
        /// Custom View Catalog Implementation
        /// </summary>
        /// <returns></returns>
        protected abstract IViewCatalog CreateViewCatalog();
        #endregion

        #region Configure container
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();

            // Custom Url Frame Navigation Service
            this.RegisterTypeIfMissing(typeof(IUltimateNavigationFrameLoadingService), typeof(UltimateNavigationFrameLoadingService), true);
            this.RegisterTypeIfMissing(typeof(IUltimateRegionNavigationJournal), typeof(UltimateRegionNavigationJournal), false);

            // Overrides the IRegionNavgationService with the custom frame one
            this.Container.RegisterType<IRegionNavigationService, UltimateRegionNavigationService>();

            // Register View Catalog Instance
            this.Container.RegisterInstance(CreateViewCatalog());

            this.RegisterTypeIfMissing(typeof(IUltimateModuleRegistrationService), typeof(UltimateModuleRegistrationService), true);
            this.RegisterTypeIfMissing(typeof(IUltimateModuleLoadingService), typeof(UltimateModuleLoadingService), true);
            this.RegisterTypeIfMissing(typeof(IUltimateViewLoadingService), typeof(UltimateViewLoadingService), true);
        }
        #endregion

        #region Configure Region Adapter Mappings
        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            var factory = ServiceLocator.Current.GetInstance<IRegionBehaviorFactory>();

            RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
            mappings.RegisterMapping(typeof(UltimateNavigationFrame), new UltimateNavigationFrameRegionAdapter(factory));

            return mappings;
        }
        #endregion
    }
    #endregion
}
